var ns, app;
app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        selectTime: 0,//0 接单时间   1 完成时间
        TypeList: [
            { ID: 0, Name: '全部类型' },
            { ID: 1, Name: '洗衣配送' },
            { ID: 2, Name: '快递配送' },
            { ID: 3, Name: '商城配送' },
        ],
        form: {
            pageindex: 0,
            pagesize: 10,
            startTime: '',
            endTime: '',
            PsTypes: [],
            timeType: 1,//1=接单日期 2=完成日期
        },
        total: {
            currentCount: 0,
            totalCount: 0,//累计完成
        },
        list: [],
        // list:[
        //     {"id":1,"customerName":"李文静","customerPhone":"18711111111","OrderStatus":1,"OrderStatusDesc":"派送中","status":5,"statusDesc":"送货上门","fromAddress":"社区配送站-北大门收货点","toAddress":"东岸国际-蔷薇馆-1栋1单元-109","appointTime":"今日18:00-18:30","goodWeight":"4.79","isTimingOut":'即将超时',"remark":"都是买的水果，请轻拿轻放，谢谢",orderItemList:[]},
        //     {"id":2,"customerName":"李文静1","customerPhone":"18211111111","OrderStatus":1,"OrderStatusDesc":"待取衣","status":3,"statusDesc":"上门取货","fromAddress":"社区配送站-北大门收货点","toAddress":"东岸国际-蔷薇馆-1栋1单元-109","appointTime":"今日18:00-18:30","goodWeight":"","isTimingOut":'',"remark":"",orderItemList:[]},
        //     {"id":3,"customerName":"李文静2","customerPhone":"18511111111","OrderStatus":1,"OrderStatusDesc":"派送中","status":2,"statusDesc":"快递配送","fromAddress":"社区配送站-北大门收货点","toAddress":"东岸国际-蔷薇馆-1栋1单元-109","appointTime":"今日18:00-18:30","goodWeight":"","isTimingOut":'',"remark":"都是买的水果，请轻拿轻放，谢谢",orderItemList:[]},
        // ],   
        current_item_length: 0,
        can_scroll: false,
        scroll_top: 0,
        now_search_data: '',
    },
    methods: {
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },

        doReset: function (type) {
            var that = this;
            that.form.PsTypes = [];
            that.form.startTime = '';
            that.form.endTime = '';
        },
        doSearch: function () {
            var that = this;
            that.doHideSearch();
            that.get_data();
        },
        get_data: function () {
            var that = this;
            setTimeout(() => {
                that.getData();
            }, 300);
        },
        doSelectPSType: function (ID) {
            var that = this;
            var index = that.form.PsTypes.indexOf(ID);
            if (index > -1) {
                that.form.PsTypes.splice(index, 1);
            } else {
                that.form.PsTypes.push(ID);
            }
        },
        doSelectTime: function (type) {
            var that = this;
            that.form.timeType = type;
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            if (type == 1 && that.showsearchparam) {
                setTimeout(function () {
                    that.initPicker();
                }, 100);
            }
        },
        do_open: function (item) {
            var that = this;
            ns.openWin('distribution_crenter_detail', '详情', {
                ID: item.id
            });
        },
        tellCallPhone: function (PnoneNumber) {
            if (!PnoneNumber) {
                api.toast({
                    msg: '联系电话为空',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.confirm({
                title: '提示',
                msg: '确定拨打电话',
                buttons: ['确定', '取消']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        api.call({
                            type: 'tel_prompt',
                            number: PnoneNumber
                        });
                    };
                };
            });
        },
        getData: function () {
            var that = this;
            that.can_scroll = false;
            var options = {};
            options.pageSize = that.form.pagesize;
            options.pageIndex = that.form.pageindex;
            options.status = 3;
            options.types = JSON.stringify(that.form.PsTypes);
            options.timeType = that.form.timeType;
            options.startTime = that.form.startTime;
            options.endTime = that.form.endTime;
            options.action = "getdeliverylist";
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                if (succeed) {
                    // that.list=data.response;
                    if (data.response.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.response;
                    } else {
                        that.list = that.list.concat(data.response);
                    }
                    that.current_item_length = that.list.length;
                    that.total.currentCount = data.currentCount;
                    that.total.totalCount = data.totalCount;

                } else if (err) {
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //开始日期
                    var btn = document.querySelector('#startdate');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.startTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.startTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                    //结束日期
                    var btnEnd = document.querySelector('#enddate');
                    btnEnd.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.endTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.endTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                });
            })(mui, document);
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;

    var nowDate = new Date();
    var Y = nowDate.getFullYear();
    var M = nowDate.getMonth() + 1 < 10 ? '0' + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
    var D = nowDate.getDate() < 10 ? '0' + nowDate.getDate() : nowDate.getDate();
    app.form.startTime = Y + '-' + M + '-' + D;//初始化默认当天
    app.form.endTime = Y + '-' + M + '-' + D;//初始化默认当天
    app.now_search_data = Y + '-' + M + '-' + D;

    app.doSelectPSType(0);

    app.get_data();
    setTimeout(function () {
        ns.init_pull();
    }, 500)


};
